<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

$user_id = ( ! empty($_GET['id'])) ? $_GET['id'] : $_SESSION['uid'] ;

// Recupera il nome del personaggio
if($r1 = $db -> query("SELECT user_name, user_surname FROM users WHERE user_id = '$user_id'"))
{
	$rd1 = $r1 -> fetch_assoc();
	
	$user_name = $rd1['user_name'];
	$user_surname = $rd1['user_surname'];
	
	$r1 -> close();
}

// Variabili
$msg_box = '';
$content = '';

// Recupera i logs relativi al personaggio.
if($r2 = $db -> query("SELECT * FROM logs WHERE log_type = 1 AND log_target = '$user_id'"))
{
	
	while($rd2 = $r2 -> fetch_assoc())
	{
		// Variabili
		$from_id	= $rd2['user_id'];
		$log_size	= $rd2['log_size'];
		$log_msg	= trim(mysql_real_escape_string($rd2['log_msg']));
		$log_date	= date('d-m-Y H:i', strtotime($rd2['log_date']));
		
		// Recupera il nome di chi ha effettuato l'azione.
		if($r3 = $db -> query("SELECT user_name FROM users WHERE user_id = '$from_id'"))
		{
		
			$rd3 = $r3 -> fetch_assoc();
			
			// Variabili
			$from_name = trim(mysql_real_escape_string($rd3['user_name']));
			
			// Crea la lista-
			$content .= '<tr><td>' . $log_msg . '</td><td>' . $log_date . '</td><td>' . $log_size . '</td><td><a href="avatar.php?id=' . $from_id . '" target="_self">' . $from_name . '</a></td></tr>';
		
		}
	}
	
} else {
	
	// Stampa un messaggio di errore
	$msg_box .= '<div class="alert alert-danger">';
	$msg_box .= 'Impossibile identificare il personaggio.';
	$msg_box .= '</div>';

}	
	
	// Lista delle tag speciali
	$tags = array(
					'temp_title' => $g_name,'temp_content' => $content, 'temp_name' => $user_name, 
					'temp_surname' => $user_surname, 'temp_msgbox' => $msg_box, 'temp_userid' => $user_id
				);
				
	// Carica il template e formatta le tag speciali
	$temp = new template('style/avatar-logs.template.html');
	$temp -> replace($tags);
	
	// Visualizza l'output
	echo $temp -> show();
	
	// Chiude la connessione al database
	$db -> close();

?>